<%@ Page Language="Boo" %>
<script runat="server">
	_title as string = "Banshee Web UI"

	def Page_Load( sender as object, e as EventArgs ):
		q = GetBansheePlayer()
		UpdateStatus()
		csmType = self.GetType()
		csmScriptName = "SetTimeoutScript"
		
		csm = Page.ClientScript
		if not csm.IsClientScriptBlockRegistered( csmType, csmScriptName ):
			sbScript = StringBuilder()
			sbScript.Append("function refresh() {")
			sbScript.Append( Environment.NewLine )
			sbScript.Append("$(\"#Refresh\").click();")
			sbScript.Append( Environment.NewLine )
			sbScript.Append("}")
			sbScript.Append( Environment.NewLine )
			timeout = GetTimeout()
			if timeout == 0:
				timeout = 30*1000  // 30 seconds
			sbScript.Append("""setTimeout( "refresh();", ${timeout});""")
			sbScript.Append( Environment.NewLine )
			csm.RegisterClientScriptBlock( csmType, csmScriptName, sbScript.ToString(), true )

		if not IsPostBack:
			shuffleModes = GetShuffleModes() 
			ShuffleModeCombo.DataSource = shuffleModes
			ShuffleModeCombo.DataBind()

			repeatModes = GetRepeatModes() 
			RepeatModeCombo.DataSource = repeatModes
			RepeatModeCombo.DataBind()

			ShuffleModeCombo.SelectedIndex = GetIndex( shuffleModes, q.ShuffleMode )
			RepeatModeCombo.SelectedIndex = GetIndex( repeatModes, q.RepeatMode )

	#region Helper functions
	def GetIndex( list, selected ) as int:
		index=0
		found = false
		for s in list:
			if s==selected:
				found=true
				break
			index++
		index=0 if not found
		return index
	
	def UpdateStatus():
		q = GetBansheePlayer()
		if q.Playing:
			self.Title = "Now playing ${q.NowPlayingTitle} - ${_title}"
			Message.Text = "Now Playing"
			Toggle.Text = "Pause"
		else:
			self.Title = "Stopped - ${_title}"
			Message.Text = "No Track Selected"
			Toggle.Text = "Play"
		Artist.Text = q.NowPlayingArtist
		Album.Text = q.NowPlayingAlbum
		TrackTitle.Text = q.NowPlayingTitle

	def GetBansheePlayer() as IBansheePlayer:
		obj = AppDomain.CurrentDomain.GetData( "bansheePlayer" )
		q as IBansheePlayer = null
		if obj!=null:
			q = obj as IBansheePlayer
		else:
			q = NullBansheePlayer()
		return q

	def Delay( f as ICallable ):
		for i in range(3):
			if f:
				System.Threading.Thread.Sleep(500)
			else:
				break

	def GetTimeout() as int:
		q  = GetBansheePlayer()
		totalMs as uint = Convert.ToUInt32( q.NowPlayingDuration.TotalMilliseconds )
		return totalMs - q.NowPlayingPosition
	#endregion
	
	#region Events
	def Toggle_Clicked( sender as object, e as EventArgs ):
		q  = GetBansheePlayer()
		isPlaying = q.Playing
		q.TogglePlaying()
		Delay( { return q.Playing==isPlaying } )
		UpdateStatus()

	def Previous_Clicked( sender as object, e as EventArgs ):
		q  = GetBansheePlayer()
		q.PlayPrevious()
		Delay( { return q.NowPlayingTitle == TrackTitle.Text } )
		UpdateStatus()

	def Next_Clicked( sender as object, e as EventArgs ):
		q  = GetBansheePlayer()
		q.PlayNext()
		Delay( { return q.NowPlayingTitle == TrackTitle.Text } )
		UpdateStatus()

	def Refresh_Clicked( sender as object, e as EventArgs ):
		UpdateStatus()

	def ShuffleModeCombo_SelectIndexChanged( sender as object, e as EventArgs ):
		q  = GetBansheePlayer()
		q.ShuffleMode = ShuffleModeCombo.SelectedValue
	
	def RepeatModeCombo_SelectIndexChanged( sender as object, e as EventArgs ):
		q  = GetBansheePlayer()
		q.RepeatMode = RepeatModeCombo.SelectedValue
	#region
</script><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head runat="server">
		<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
		<title>Banshee Web UI</title>
		<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.5.2/build/reset-fonts-grids/reset-fonts-grids.css">
		<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.5.2/build/base/base-min.css">
		<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.js"></script>
		<script>
			$(document).ready( function() {
				$("#btnTest").click( function() {
					$.get( "/bansheeWebService.asmx/Add", 
						{ a:10, b:20 },
						function( data ) {
							alert( $("int", data).text() );
						}
					);
				} );
			});
		</script>
		<style>
		form p label {
			float: left;
			width: 15%;
		}
		form p span {
			clear: left;
		}
		</style>
	</head>
	<body>
		<div id="doc" class="yui-t7">
			<div id="hd"><h1>Banshee Web UI</h1></div>
			<div id="bd">
				<div class="yui-g">
					<form runat="server">
						<p>
							<h2><asp:Label id="Message" runat="server"/></h2>
						</p>
						<p>
							<label>Artist:</label><asp:Label id="Artist" runat="server"/>
						</p>
						<p>
							<label>Album:</label><asp:Label id="Album" runat="server"/>
						</p>
						<p>
							<label>Title:</label><asp:Label id="TrackTitle" runat="server"/>
						</p>
						<p>
							<asp:Button ID="Toggle" runat="server" OnClick="Toggle_Clicked" Text=""></asp:Button>
							<asp:Button ID="Previous" runat="server" OnClick="Previous_Clicked" Text="Previous Track"></asp:Button>
							<asp:Button ID="Next" runat="server" OnClick="Next_Clicked" Text="Next Track"></asp:Button>
							<asp:Button ID="Refresh" runat="server" OnClick="Refresh_Clicked" Text="Refresh"></asp:Button>
							<input type="button" id="btnTest" value="Web service Test"/>
						</p>
						<p>
							<label>Shuffle Mode:</label><asp:DropDownList id="ShuffleModeCombo" AutoPostBack="True" OnSelectedIndexChanged="ShuffleModeCombo_SelectIndexChanged" runat="server"/>
						</p>
						<p>
							<label>Repeat Mode:</label><asp:DropDownList id="RepeatModeCombo" AutoPostBack="True" OnSelectedIndexChanged="RepeatModeCombo_SelectIndexChanged" runat="server"/>
						</p>
					</form>
				</div>
			</div>
			<div id="ft">Powered by Mono, Banshee, Banshee.BooScript, Boo</div>
		</div>
	</body>
</html>

